import 'package:flutter/material.dart';
import 'package:kasax/utils/log_utils.dart';

/// 补间动画
class AnimTweenWidget extends StatefulWidget {
  const AnimTweenWidget({Key? key}) : super(key: key);

  @override
  _AnimTweenWidgetState createState() => _AnimTweenWidgetState();
}

class _AnimTweenWidgetState extends State<AnimTweenWidget> with TickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation animmSize;
  late Animation animColor;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 3000));
    animmSize = Tween(begin: 32.0, end: 220.0).animate(_animationController);
    animColor = ColorTween(begin: Colors.deepOrange, end: Colors.redAccent).animate(_animationController);
    _animationController.addListener(() {
      setState(() {});
    });
    _animationController.addStatusListener((status) {
      LogUtil.e("$status");
      switch (status) {
        case AnimationStatus.forward:
          LogUtil.e("动画开始执行了");
          break;
        case AnimationStatus.completed:
          LogUtil.e("动画执行完成了");
          break;
        case AnimationStatus.dismissed:
          LogUtil.e("动画取消了");
          break;
        case AnimationStatus.reverse:
          LogUtil.e("动画反转");
          break;
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: IconButton(
          onPressed: () {
            switch (_animationController.status) {
              case AnimationStatus.completed:
                // 反转执行动画
                _animationController.reverse();
                break;
              default:
                // 执行动画
                _animationController.forward();
                break;
            }
          },
          iconSize: animmSize.value, // 动画的值作为图片的尺寸
          icon: Icon(Icons.star),
          color: animColor.value, // 动画的值作为图片的颜色
        ),
      ),
    );
  }
}
